<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="保养标题" prop="title">
				<el-input v-model="form.title" placeholder="保养标题" maxlength="80" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="车牌号码" prop="vehicleId">
						<sc-select v-model="form.vehicleId" :apiObj="$API.office.vehicle.getVehicleListForSelect" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="保养类型" prop="repairType">
						<sc-select v-model="form.repairType" :params="{code:'repairType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="经办人" prop="repairUser">
						<select-user-dialog v-model:accountId="form.repairUser" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="维修时间" prop="repairTime">
						<el-date-picker v-model="form.repairTime" placeholder="维修时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="保养原因" prop="reason">
				<el-input v-model="form.reason" placeholder="保养原因" maxlength="300" type="textarea" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="供应商" prop="repairSupplier">
						<el-input v-model="form.repairSupplier" placeholder="供应商" maxlength="80" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="保养金额" prop="repairPay">
						<el-input-number v-model="form.repairPay" placeholder="保养金额" clearable></el-input-number>/元
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明" prop="remark">
				<el-input v-model="form.remark" placeholder="备注说明" maxlength="300" type="textarea" clearable></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'vehicle'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '创建保养记录',
				edit: '编辑保养记录',
			},
			apiObj: this.$API.file.attach.uploadFile,
			fileList: [],
			form: {
				recordId: '',
				vehicleId: '',
				title: '',
				repairType: '',
				repairUser: '',
				repairTime: '',
				reason: '',
				repairSupplier: '',
				repairPay: 0,
				attachId: '',
				remark: '',
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				title: [
					{required: true, message: '保养标题不能为空', trigger: 'blur'}
				],
				reason: [
					{required: true, message: '保养原因不能为空', trigger: 'blur'}
				],
				repairUser: [
					{required: true, message: '保养人不能为空', trigger: 'blur'}
				],
				repairPay: [
					{required: true, message: '保养金额不能为空', trigger: 'blur'}
				],
				repairSupplier: [
					{required: true, message: '供应商不能为空', trigger: 'blur'}
				],
				repairTime: [
					{required: true, message: '保养时间不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.vehicle.insertVehicleRepairRecord.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.vehicle.updateVehicleRepairRecord.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.vehicleId = data.vehicleId
			this.form.title = data.title
			this.form.repairType = data.repairType
			this.form.repairUser = data.repairUser
			this.form.repairTime = data.repairTime
			this.form.reason = data.reason
			this.form.repairSupplier = data.repairSupplier
			this.form.repairPay = data.repairPay
			this.form.attachId = data.attachId
			this.form.remark = data.remark
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
